package tuan.travel;

import java.util.*;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;


public class PrintTravel implements TravelCallback {
	Stack<Node> pathStack = new Stack<Node>();
	int pathLen = 0;
	boolean hasInited = false;

	public void callback(Node root, Node node) {
		String text = getIndent(root, node) + node.getNodeName();
		NamedNodeMap attrList = node.getAttributes();
		for (int i = 0; attrList != null && i < attrList.getLength(); i++) {
			Node aNode = attrList.item(i);
			text += aNode.getNodeName() + "=" + aNode.getNodeValue();
		}
		if (node.getNodeType() == Node.TEXT_NODE) {
			text += "("+node.getTextContent().trim() + ")";
		}
			

		System.out.println(text);
	}

	String getIndent(Node root, Node node) {
		String indent = "";
		int height = 0;
		node = node.getParentNode();
		while (node != null && !node.equals(root)) {
			height++;
			node = node.getParentNode();
		}
		for (int i = 0; i < height; i++) {
			indent += "  ";
		}
		return indent;
	}
}
